package com.amita.ez.web;

import com.amita.ez.base.R;
import com.amita.ez.entity.SysUser;
import com.amita.ez.entity.VwUserMenu;
import com.amita.ez.service.ISysMenuService;
import com.amita.ez.service.ISysUserService;
import com.amita.ez.service.IUserInfoService;
import com.amita.ez.service.IVwUserMenuService;
import com.amita.ez.vo.MenuVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api")
//@Tag(name = "账户信息表")
public class AmitaCtrl extends BaseCtrl {



    @Autowired
    IUserInfoService userInfoService;

    @Autowired
    ISysUserService sysUserService;

    @Autowired
    ISysMenuService sysMenuService;

    @Autowired
    IVwUserMenuService vwUserMenuService;




//    @Autowired
//    StructUtil structUtil;
    @Autowired
    Mapper mapper;



    /**
     * 添加数据
     * @param uname, upass
     * @return
     */
    @PostMapping("/login")
    public Object login (String uname, String upass){

//
//        SysUser loginUser = new SysUser();
//        loginUser.setUsername(uname);
//        loginUser.setPassword(upass);

        QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUser::getEnabled, true).eq(SysUser::getUsername, uname)
                .eq(SysUser::getPassword, upass);
        List<SysUser> userInfoList = sysUserService.list(wrapper);
        if(!userInfoList.isEmpty()){
            SysUser sysUser = userInfoList.get(0);
            String enabled = sysUser.getEnabled();
            if("1".equals(enabled)){
                sysUser.setPassword(null);
                String token = UUID.randomUUID().toString().replace("-","");
                userInfoService.put(token, sysUser);
                return R.ok(token);
            }
        }


        return ResponseEntity.status(203).body(R.failed("login failed"));
    }

    @GetMapping("/me")
    public Object me(@RequestHeader("Authorization") String token){
//        return userInfoService.getByToken(token);
        return  getLoginUser();
    }

    @GetMapping("/build/menus")
    public Object buildMenus(){
        SysUser loginUser = getLoginUser();
        Long userId = loginUser.getOid();

        List<VwUserMenu> menus =vwUserMenuService.list(new LambdaQueryWrapper<VwUserMenu>().eq(VwUserMenu::getUserId, userId).in(VwUserMenu::getType, 0, 1).orderByAsc(VwUserMenu::getMenuSort));
//        List<VwUserMenu> menus =vwUserMenuService.list();

        for (VwUserMenu menu : menus) {
            if(menu.getType() == 0){
                menu.setComponent("Layout");

            }
        }

        List<MenuVO> menuVOList = menus.stream().map(el -> mapper.map(el, MenuVO.class)).collect(Collectors.toList());
        List<MenuVO> roots = menuVOList.stream().filter(el-> el.getPid() == null).collect(Collectors.toList());
        List<MenuVO> rests = menuVOList.stream().filter(el -> el.getPid() != null).collect(Collectors.toList());

        buildTree(roots, rests);
        return roots;
    }

    private void buildTree(List<MenuVO> roots, List<MenuVO> rests) {

        if(roots.isEmpty() || rests.isEmpty()){
            return ;
        }

        for (MenuVO root : roots) {
            Long oid = root.getOid();
            List<MenuVO> children = rests.stream().filter(el -> el.getPid() == oid).collect(Collectors.toList());
            if(children.size() >0 ){
                root.setAlwaysShow(true);
                root.setChildren(children);
                List<MenuVO> others = rests.stream().filter(el -> el.getPid() != oid).collect(Collectors.toList());
                buildTree(children, others);
            }


        }

    }


}
